package com.company.cloud.platform.modules.mgr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.platform.modules.cache.models.AppAuthUrlModel;
import com.company.cloud.platform.modules.mgr.controller.req.*;
import com.company.cloud.user.modules.api.request.AuthUrlConfigAddRequest;
import com.company.cloud.user.modules.api.request.AuthUrlConfigGet2DelRequest;
import com.company.cloud.user.modules.api.request.AuthUrlConfigQueryRequest;
import com.company.cloud.user.modules.api.request.AuthUrlConfigUpdateRequest;
import com.company.cloud.user.modules.api.response.AuthUrlConfigQueryResp;
import com.company.cloud.user.modules.api.response.AuthUrlConfigResp;
import com.company.cloud.user.modules.api.service.UrlAuthPlatformRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 平台安全权限URL配置
 */
@Authc
@RestController
@RequestMapping("/admin/mgr/authUrlConfig")
@Api(tags="Admin_MGR_平台安全权限URL配置")
public class UrlAuthConfigMgrController {


	@Autowired
	private UrlAuthPlatformRemoteService urlAuthPlatformRemoteService;

	@PostMapping("/query")
	@ApiOperation(value="查询", response= AuthUrlConfigResp.class)
	public BaseResult query(@RequestBody AppAuthQuery4PageParam params) {
		AuthUrlConfigQueryRequest request=new AuthUrlConfigQueryRequest();
		request.setPage(params.getPage());
		request.setRows(params.getRows());
		request.setServiceName(params.getServiceName());
		request.setServicePath(params.getServicePath());
		request.setMethodUrl(params.getMethodUrl());
		request.setAuthType(params.getAuthType());
		request.setIsAuto(params.getIsAuto());
		BaseClientResult<AuthUrlConfigQueryResp> authUrlConfigQueryResp = urlAuthPlatformRemoteService.queryAuthUrlConfig(request);
		AuthUrlConfigQueryResp resp = BaseClientResult.getResultData2Error(authUrlConfigQueryResp);

		IPage<AuthUrlConfigResp> data = new Page<AuthUrlConfigResp>();
		data.setRecords(resp.getRecords());
		data.setTotal(resp.getTotal());
		data.setPages(resp.getPages());
		data.setSize(resp.getSize());
		data.setCurrent(resp.getCurrent());
		return BaseResult.ok(data);
	}

	@GetMapping("/get")
	@ApiOperation(value="获取", response=AuthUrlConfigResp.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		AuthUrlConfigGet2DelRequest request=new AuthUrlConfigGet2DelRequest();
		request.setId(id);
		BaseClientResult<AuthUrlConfigResp> authUrlConfigResp= urlAuthPlatformRemoteService.getAuthUrlConfig(request);
		AuthUrlConfigResp data=BaseClientResult.getResultData2Error(authUrlConfigResp);
		return BaseResult.ok(data);
	}

	@PostMapping("/add")
	@ApiOperation(value="新增", response=BaseResult.class)
	@StaffLogc(title = "新增安全权限URL配置", action = "新增安全权限URL配置", actionAdo = "平台安全权限URL配置")
	public BaseResult add(@RequestBody AuthUrlConfigAddRequest request) {
		ValidatorUtils.validate(request);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		request.setOpIp(userInfo.getIp());
		request.setOpUserId(userInfo.getId());
		urlAuthPlatformRemoteService.addAuthUrlConfig(request);
		return BaseResult.ok();
	}

	@PostMapping("/edit")
	@ApiOperation(value="修改", response=BaseResult.class)
	@StaffLogc(title = "修改安全权限URL配置", action = "修改安全权限URL配置", actionAdo = "平台安全权限URL配置")
	public BaseResult edit(@RequestBody AuthUrlConfigUpdateRequest params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		params.setOpIp(userInfo.getIp());
		params.setOpUserId(userInfo.getId());
		urlAuthPlatformRemoteService.updateAuthUrlConfig(params);
		return BaseResult.ok();
	}

	@GetMapping("/del")
	@ApiOperation(value="删除", response=BaseResult.class)
	@StaffLogc(title = "删除安全权限URL配置", action = "删除安全权限URL配置", actionAdo = "平台安全权限URL配置")
	public BaseResult del(@RequestParam("id")Integer id) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		AuthUrlConfigGet2DelRequest request=new AuthUrlConfigGet2DelRequest();
		request.setId(id);
		request.setOpIp(userInfo.getIp());
		request.setOpUserId(userInfo.getId());
		urlAuthPlatformRemoteService.delAuthUrlConfig(request);
		return BaseResult.ok();
	}
	
}
